using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace AntidoteEngine
{
    class Layer
    {
        List<Sprite> LayerSprites;
        bool offset;

        public int Count
        {
            get { return LayerSprites.Count; }
        }

        public void Add(Sprite newsprite)
        {
            LayerSprites.Add(newsprite);
        }

        public bool Offset
        {
            get { return offset; }
        }

        public List<Sprite> GetList
        {
            get { return LayerSprites; }
        }

        public Sprite RemoveAt(int index)
        {
            Sprite returnsprite;
            if (index >= 0 && index < LayerSprites.Count)
            {
                returnsprite = LayerSprites[index];
                LayerSprites.RemoveAt(index);

                return returnsprite;
            }

            return null;
        }

        public Layer(bool off)
        {
            LayerSprites = new List<Sprite>();
            offset = off;
        }
    }

    class SpriteRenderManager
    {
        List<Layer> SpriteLayers;
        SpriteBatch SpriteMngrBatch;
        
        public SpriteRenderManager()
        {
            SpriteLayers = new List<Layer>();
            SpriteMngrBatch = new SpriteBatch(GraphicsManager.GraphicsDevice);
        }

        public bool AddDrawSprite(int LayerNumber, Sprite newdrawsprite)
        {
            if (LayerNumber >= 0 && LayerNumber < SpriteLayers.Count)
            {
                SpriteLayers[LayerNumber].GetList.Add(newdrawsprite);
                return true;
            }

            return false;
        }

        public bool RemoveSprite(int LayerNumber, int index)
        {
            if (LayerNumber >= 0 && LayerNumber < SpriteLayers.Count)
            {
                if (index >= 0 && index < SpriteLayers[LayerNumber].Count)
                {
                    SpriteLayers[LayerNumber].GetList.RemoveAt(index);
                    return true;
                }
            }

            return false;
        }

        public void RenderSprites()
        {
            Vector2 Offset = GraphicsManager.SpriteOffset;
            SpriteMngrBatch.Begin();

            for (int l = 0; l < SpriteLayers.Count; l++)
            {
                Vector2 offset;

                if (SpriteLayers[l].Offset)
                    offset = Offset;
                else
                    offset = Vector2.Zero;

                for (int s = 0; s < SpriteLayers[l].Count; s++)
                {
                    Sprite CurrentSprite = SpriteLayers[l].GetList[s];
                    if (!CurrentSprite.HideSprite)
                    {
                        Vector2 Position = CurrentSprite.Position;

                        //Here should go scaling the sprite based on resolution

                        SpriteMngrBatch.Draw(CurrentSprite.Image, CurrentSprite.Position,
                                             CurrentSprite.SourceRectangle, CurrentSprite.SpriteColor,
                                             CurrentSprite.Rotation, CurrentSprite.RotationOrigin,
                                             CurrentSprite.Scale, CurrentSprite.SpriteEffect, 1f);
                    }
                }
            }
        }
    }
}
